package com.wiim.connector;

import java.net.URI;

import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.ResponseExtractor;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import android.util.Log;

public class StateRestTemplate extends RestTemplate {

	private final DefaultHttpClient httpClient;
	private final CookieStore cookieStore;
	private final HttpContext httpContext;
	private final StateHttpComponentsClientHttpRequestFactory stateHttpComponentsClientHttpRequestFactory;

	public StateRestTemplate() {
		super();
		httpClient = new DefaultHttpClient();
		cookieStore = new BasicCookieStore();
		httpContext = new BasicHttpContext();
		httpContext.setAttribute(ClientContext.COOKIE_STORE, getCookieStore());

		stateHttpComponentsClientHttpRequestFactory = new StateHttpComponentsClientHttpRequestFactory(
				httpClient, httpContext);
		super.setRequestFactory(stateHttpComponentsClientHttpRequestFactory);

		/*
		 * // Set the username and password for creating a Basic Auth request
		 * HttpAuthentication authHeader = new HttpBasicAuthentication(username,
		 * password); HttpHeaders requestHeaders = new HttpHeaders();
		 * requestHeaders.setAuthorization(authHeader); HttpEntity<?>
		 * requestEntity = new HttpEntity<Object>(requestHeaders);
		 * 
		 * // Create a new RestTemplate instance RestTemplate restTemplate = new
		 * RestTemplate();
		 * 
		 * // Add the String message converter
		 * restTemplate.getMessageConverters().add(new
		 * StringHttpMessageConverter());
		 * 
		 * try { // Make the HTTP GET request to the Basic Auth protected URL
		 * ResponseEntity<Message> response = restTemplate.exchange(url,
		 * HttpMethod.GET, requestEntity, String.class); return
		 * response.getBody(); } catch (HttpClientErrorException e) { Log.e(TAG,
		 * e.getLocalizedMessage(), e); // Handle 401 Unauthorized response }
		 */

	}

	public HttpClient getHttpClient() {
		return httpClient;
	}

	public CookieStore getCookieStore() {
		return cookieStore;
	}

	public HttpContext getHttpContext() {
		return httpContext;
	}

	public StateHttpComponentsClientHttpRequestFactory getStatefulHttpClientRequestFactory() {
		return stateHttpComponentsClientHttpRequestFactory;
	}

	@Override
	protected <T> T doExecute(URI arg0, HttpMethod arg1, RequestCallback arg2,
			ResponseExtractor<T> arg3) {

		// TODO Auto-generated method stub
		try {
			return super.doExecute(arg0, arg1, arg2, arg3);
		} catch (RestClientException ex) {
			Log.e("StateRestTemplate", "doExecute:" + ex.getMessage());
			return null;
		}
	}
}
